package application;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import kernel.Disk;
import kernel.FileManage;
import kernel.FileMsg;
import kernel.Oftle;
import uiTip.UiTip;

public class SamplePane implements Initializable
{
	@FXML
	private Button btnBack;//后退按钮

	@FXML
	private Button btnNewBuild;//新建按钮
	@FXML
	private TableView<Oftle> tableView;
	@FXML
	private TableColumn colFirst;
	@FXML
	private TableColumn colSecond;
	@FXML
	private FlowPane rootPane;//这里设为static会得到null，设为public用new SamplePane得到的也是null

	public static Stage stage;//新建窗口

	@Override
	public void initialize(URL location, ResourceBundle resources)
	{
		rootPane.setHgap(35);
		rootPane.setVgap(35);
		rootPane.setPadding(new Insets(30,30,30,30));
		new Contain(rootPane);//把rootPane传过去

		// 初始化
		Disk.start();
		FileMsg roo = new FileMsg("roo".getBytes(), "  ".getBytes(), (byte) 8, (byte) 2, (byte) 0);
		Contain.path.add(roo);

		btnNewBuild.setOnAction(e -> {
			stage = new Stage();
			Parent root = null;
			try
			{
				root = FXMLLoader.load(getClass().getResource("/resources/Build.fxml"));
			} catch (IOException e1)
			{
				e1.printStackTrace();
			}
			stage.setTitle("新建");
			stage.setScene(new Scene(root));
			stage.show();
		});
		
		btnBack.setOnAction(e->{
			
			if(Contain.path.size()==1) {
				UiTip.tip("当前已经是根目录，无法后退。");
				return;
			}
			Contain.path.remove(Contain.path.size()-1);
			
			
			HashMap<FileMsg, Button> map=FileManage.dir(Contain.path, Contain.map);;
			Set<Map.Entry<FileMsg, Button>> entryAll = Contain.map.entrySet();
			for(Map.Entry<FileMsg, Button> ent:entryAll) {//撤下所有图标
				ent.getValue().setVisible(false);
				ent.getValue().setManaged(false);
			}
			
			Set<Map.Entry<FileMsg, Button>> entry = map.entrySet();
			for(Map.Entry<FileMsg, Button> ent:entry) {//放上当前目录下的文件/文件夹的图标
				ent.getValue().setVisible(true);
				ent.getValue().setManaged(true);
			}
		});
		
		
		/////////////
//		tableView.setEditable(false);
		tableView.setItems(FileManage.getOfList().getOpenedFilesTable());
		colFirst.setCellValueFactory(new PropertyValueFactory("nameP"));
		colSecond.setCellValueFactory(new PropertyValueFactory("flagP"));
		
	}
}
